/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.common;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Thinh Hung
 */
public class MailUtil {

    private String smtpHost;
    private String smtpAuth;
    private String smtpType;
    private String smtpPort;
    private String smtpUser;
    private String smtpPass;
    private String supportEmail;
    private String supportName;

    public MailUtil() {
        ConfigUtil configUtil = new ConfigUtil();
        this.smtpHost = configUtil.getConfig(ConfigUtil.SMTP_HOST);
        this.smtpAuth = configUtil.getConfig(ConfigUtil.SMTP_AUTH);
        this.smtpType = configUtil.getConfig(ConfigUtil.SMTP_TYPE);
        this.smtpPort = configUtil.getConfig(ConfigUtil.SMTP_PORT);
        this.smtpUser = configUtil.getConfig(ConfigUtil.SMTP_USERNAME);
        this.smtpPass = configUtil.getConfig(ConfigUtil.SMTP_PASSWORD);
        this.supportEmail = configUtil.getConfig(ConfigUtil.SUPPORT_EMAIL);
        this.supportName = configUtil.getConfig(ConfigUtil.SUPPORT_NAME);
    }

    /**
     * Send mail to support email
     * 
     * @param subject
     * @param content
     * @return
     */
    public boolean sendMail(String subject, String content) {
        return sendMail(subject, content, this.supportEmail);
    }


    /**
     * Send mail to recipients
     * 
     * @param subject
     * @param content
     * @param recipients
     * @return
     */
    public boolean sendMail(String subject, String content, String recipients) {
        Properties props = new Properties();
        props.put("mail.smtp.host", this.smtpHost);
        
        // SSL
        if (this.smtpType.equals("SSL")) {
            props.put("mail.smtp.socketFactory.port", this.smtpPort);
            props.put("mail.smtp.socketFactory.class",
                    "javax.net.ssl.SSLSocketFactory");
        } else if(this.smtpType.equals("TLS")) { //TLS
            props.put("mail.smtp.starttls.enable", "true");
        } else {
            return false;
        }
        props.put("mail.smtp.auth", this.smtpAuth);
        props.put("mail.smtp.port", this.smtpPort);

        Session session = Session.getDefaultInstance(props,
                new javax.mail.Authenticator() {

                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(smtpUser, smtpPass);
                    }
                });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(this.supportEmail, this.supportName));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(recipients));
            message.setSubject(subject);
            message.setContent(content, "text/html");

            Transport.send(message);

        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(MailUtil.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
        return true;
    }
}
